<?php

namespace Admin\Controller;

use Admin\Common\Category;
use Admin\Logic\CateLogic;
use Admin\Logic\StockLogic;

class StockController extends CommonController
{
    // 展示列表
    public function index()
    {
        if (!empty(I('get.goods_name_query')) || !empty(I('get.cate_id'))) {
            // 根据商品名称查询
            $stock = StockLogic::getStockByName(I('get.goods_name_query'), I('get.cate_id'));
            $goods_name_query = I('get.goods_name_query');
        } else {
            // 获取库存列表
            $stock = StockLogic::getAll();
            $goods_name_query = '';
        }
        $cate = CateLogic::getAll();
        $this->assign(['stock'=>$stock,'goods_name_query'=>$goods_name_query, 'cate' => $cate]);
        $this->display();
    }


    // 展示修改页面
    public function edit()
    {

        $id = I('get.id', 0, 'intval');

        // 获取库存信息
        $stock = StockLogic::getStockById($id);
        $stock['goods_price'] = $stock['goods_price'] / 100;
        $stock['price'] = $stock['price'] / 100;
        // 获取等级分类
        $cate = Category::toLevel(CateLogic::getShowCate());

        $this->assign(['stock'=>$stock,'cate'=>$cate]);
        $this->display();
    }

    // 展示添加页面
    public function add()
    {
        // 所有分类
        $cate = Category::toLevel(CateLogic::getShowCate());

        $this->assign(['cate'=>$cate]);
        $this->display();
    }


    /**
     * 执行修改库存的操作
     * @return string
     */
    public function update()
    {

        $data = I('post.');
        $data['is_bzq'] = $data['is_bzq']=='on'?0:1;

        // 执行修改

        $res = StockLogic::update($data);

        if($res){
            // 判断库存是否有变化
            if($data['stock_old'] != $data['stock']){
                // 添加库存操作详情log
                $result = StockLogic::isStockRecord($data['stock_old'],$data['stock'],$data);
                if($result == false){
                    return $this->ajaxError('库存操作记录添加失败');
                }
            }
            return $this->ajaxSuccess();
        }
    }

    /**
     * 执行添加库存的操作
     * @return string
     */
    public function create()
    {
        $data = I('post.');
        $data['goods_price'] = $data['goods_price'] * 100;
        $data['price'] = $data['price'] * 100;
        $data['is_bzq'] = $data['is_bzq'] == 'on' ? 0 : 1;
        $stock = M('stock')->where(['goods_name' => $data['goods_name']])->find();
        if ($stock) {
            return $this->ajaxError('库存名称已经存在，请勿重新提交');
        }
        // 添加数据
        $result = StockLogic::create($data);

        if ($result) {
            return $this->ajaxSuccess();
        } else {
            return $this->ajaxError();
        }
    }


    public function delete()
    {
        $id = I('get.id');

        $data = StockLogic::delete($id);

        if ($data > 0) {
            return $this->ajaxSuccess();
        } else {
            return $this->ajaxError('删除失败，请重试');
        }
    }


    public function detail()
    {
        $stock_id = I('get.id');
        $detail = StockLogic::detail($stock_id);
        $this->assign('detail', $detail);
        $this->display();
    }


}